package com.example.shirodemo.controller.admin;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.example.shirodemo.constant.HttpCode;
import com.example.shirodemo.dto.ResponseResult;
import com.example.shirodemo.dto.ResponseResultPage;
import com.example.shirodemo.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author 绫小路
 * @Date 2021/1/11
 */
@Api(tags = "04-后台页面：角色管理 - RoleController")
@Controller
@RequestMapping("/admin/role")
public class RoleController {

  @Autowired
  private AdminService adminService;
  @Autowired
  private HttpServletRequest request;

  @ApiOperation("后台管理获取角色列表/搜索")
  @GetMapping("/list")
  @ResponseBody
  public ResponseResultPage list(Integer page, Integer limit, String search) {
    if ((page != null && (page < 1 || page > 100)) ||
        (limit != null && (limit < 1 || limit > 100))) {
      return new ResponseResultPage(HttpCode.FAIL, "入参page、limit范围分别是：1~100、1~100");
    }
    if (page == null) {
      page = 1;
    }
    if (limit == null) {
      limit = 10;
    }
    return adminService.getRoleLists(search, page, limit);
  }

  @ApiOperation("添加角色")
  @PostMapping("/add")
  @ResponseBody
  public ResponseResult add(String name, String description) {
    if (StrUtil.isBlank(name)) {
      return new ResponseResult(HttpCode.FAIL, "角色名称不能为空");
    }
    return adminService.addRole(name, description);
  }

  @ApiOperation("删除角色")
  @PostMapping("/delete")
  @ResponseBody
  public ResponseResult delete(String name) {
    if (StrUtil.isEmpty(name)) {
      return new ResponseResult(HttpCode.FAIL, "入参name不能为空！");
    }
    return adminService.deleteRole(name);
  }

  @ApiOperation("编辑角色")
  @PostMapping("/edit")
  @ResponseBody
  public ResponseResult edit(Integer id, String name, String description) {
    Assert.isFalse(id == null, "入参id不能为空！");
    if (StrUtil.isBlank(name)) {
      return new ResponseResult(HttpCode.FAIL, "角色名称不能为空");
    }
    if (description != null && description.length() > 200) {
      return new ResponseResult(HttpCode.FAIL, "描述最长只能200个字符");
    }
    return adminService.editRole(id, name, description);
  }


  @GetMapping("permission")
  public String permission(Integer roleId) {
    Assert.isFalse(roleId == null, "入参roleId不能为空！");
    request.setAttribute("roleId", roleId);
    return "/admin/admin-role-permission";
  }

  @ApiOperation("获取角色访问授权列表")
  @GetMapping("permissionList")
  @ResponseBody
  public ResponseResultPage permissionList(Integer roleId, Integer page, Integer limit, String search) {
    Assert.isFalse(roleId == null, "入参roleId不能为空！");
    if ((page != null && (page < 1 || page > 100)) ||
        (limit != null && (limit < 1 || limit > 100))) {
      return new ResponseResultPage(HttpCode.FAIL, "入参page、limit范围分别是：1~100、1~100");
    }
    if (page == null) {
      page = 1;
    }
    if (limit == null) {
      limit = 10;
    }
    return adminService.getRolePermission(roleId, search, page, limit);
  }

  @ApiOperation("更新角色访问授权")
  @PostMapping("updatePermission")
  @ResponseBody
  public ResponseResult updatePermission(Integer roleId,String ids,Boolean checked){
    Assert.isFalse(roleId == null, "入参roleId不能为空！");
    Assert.isFalse(checked == null, "入参checked不能为空！");
    return adminService.updateRolePermission(roleId,ids,checked);
  }
}
